#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>

#define SIZE 256
#define NUM 16
int main()
{
  char cmd[SIZE];
  const char *cmd_line = "[temp@yumoz cmd ]# ";
  while(1){
    cmd[0]=0;
    printf("%s", cmd_line);
    fgets(cmd, SIZE, stdin);
    // printf("%s",cmd);
    cmd[strlen(cmd)-1]='\0';//输入命令的最后一个字符 即 \n 设为\0  

    // 拆分命令/字符串
    char *args[NUM];
    args[0] = strtok(cmd, " ");// 提取第一个空格前的字符串
    int i = 1;
    do {
      args[i] = strtok(NULL, " ");//从第二次开始划分
      if(args[i] == NULL){
        break;
      }
      ++i;
    }while(1);

    pid_t id = fork();
    if(id<0){
      perror("fork error");
      continue;
    }
    else if(id == 0){
      //child
      execvp(args[0], args); //程序替换 不需要带路径 
      exit(1);
    }
    //parent
    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(ret>0){
       //printf("status code: %d\n", (status>>8));
       printf("status code: %d\n", WEXITSTATUS(status));
    }
  }
  return 0;
}
